﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using DataModel;
using System.ServiceModel.Channels;
using System.ServiceModel;
using System.Collections.ObjectModel;
using A_ManageUsers.Services.Security;
namespace A_ManageUsers.DataModels
{
    public class Ability : DataModelBase
    {
        Services.Security.Ability A;
        public Ability(Services.Security.Ability A)
        {
            this.A = A;
        }

        public int ID { get { return A.ID; } }
        
        public string Name
        {
            get { return A.Name; }
            set
            {
                if (A.Name != value)
                {
                    A.Name = value;
                    OnPropertyChanged("Name");
                    if (value == "")
                        AddMessage(new ErrorMessage("Name", "You must enter a name for the ability", null));
                    else
                        RemoveMessage(new ErrorMessage("Name", "You must enter a name for the ability", null));
                }
            }
        }



        protected override void ComitNewObject()
        {
            UMC.NewAbilityCompleted += new EventHandler<Services.Security.NewAbilityCompletedEventArgs>(UMC_NewAbilityCompleted);
            UMC.NewAbilityAsync(A);
            Abilities.AbilitiesL.Add(this);
        }

        void UMC_NewAbilityCompleted(object sender, Services.Security.NewAbilityCompletedEventArgs e)
        {
            A.ID = e.Result;
        }

        public override string ToString()
        {
            return Name;
        }

        UserManagementClient UMC = ServiceConfig.NewUserManagementClient();
    }

    public static class Abilities
    {
        static Abilities()
        {

            UserManagementClient UMC = ServiceConfig.NewUserManagementClient();
            

            UMC.GetAbilitiesCompleted += (s, ea) =>
                {

                    foreach (var item in ea.Result)
                    {
                        AbilitiesL.Add(new Ability(item));
                    }
                };
            UMC.GetAbilitiesAsync();
        }
        public static ObservableCollection<Ability> AbilitiesL = new ObservableCollection<Ability>();
    }
}
